package com.ygo.feihua;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import com.ygo.feihua.BmobTable.MessageAlert;
import com.ygo.feihua.BmobTable.PingLun;
import java.util.List;
import android.util.*;

public class PlAdapter extends BaseAdapter
 {  

    private List<PingLun> data;  
	private List<MessageAlert> mess;  
    private Context context;  
	Zujian zujian=null;  
	ListView list;
	boolean ispl=true;;
	Gongju gj;
    public PlAdapter(Context context,List<PingLun> data,ListView list){  
	ispl=true;
        this.context=context;  
        this.data=data;  
		this.list=list;  
        gj=Gongju.getdx(context);
    }  
	
	
	public PlAdapter(Context context,List<MessageAlert> mess){  
	Log.e("PlAdapter","提醒初始化");
	ispl=false;
        this.context=context;  
        this.mess=mess;  
        gj=Gongju.getdx(context);
    }  
	
	
	public void add(PingLun map){
		data.add(map);
		notifyDataSetChanged();
	}
	
	
    /** 
     * 组件集合，对应list.xml中的控件 
     * @author Administrator 
     */  
    public final class Zujian{  
        public ImageView pl_touxiang;
		public TextView pl_name,pl_neirong,pl_lou,pl_time,pl_hfneirong;
    }  
    @Override  
    public int getCount() {  
	if(ispl){
        return data.size();  
		}else{
			return mess.size();
		}
    }  
    /** 
     * 获得某一位置的数据 
     */  
    @Override  
    public Object getItem(int position) {  
        return data.get(position);  
    }  
    /** 
     * 获得唯一标识 
     */  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  

	
    @Override  
    public View getView(final int position,  View convertView, final ViewGroup parent) {  
	
        if(convertView==null){  
            zujian=new Zujian();  
            //获得组件，实例化组件  
            convertView=LayoutInflater.from(context).inflate(R.layout.pl_item, null);  
			
			zujian.pl_touxiang=(ImageView) convertView.findViewById(R.id.pl_touxiang);
			zujian.pl_name=(TextView) convertView.findViewById(R.id.pl_name);
			zujian.pl_neirong=(TextView) convertView.findViewById(R.id.pl_neirong);	
			zujian.pl_lou=(TextView) convertView.findViewById(R.id.pl_lou);
			zujian.pl_time=(TextView) convertView.findViewById(R.id.pl_time);
			zujian.pl_hfneirong=(TextView) convertView.findViewById(R.id.pl_hfneirong);
			
            convertView.setTag(zujian);  
        }else{  
            zujian=(Zujian)convertView.getTag(); 		
			
        }  
		String hfnr = null,name,neirong,time;
		final String id;
		
		if(data!=null){
			Log.e("Pladapter","执行回复");
			final PingLun pll=data.get(position);
			if(pll.getHfneirong()!=null){
			hfnr=(pll.getHfneirong()).getneirong();
			}
			name=pll.getUser().getUsername();
			zujian.pl_lou.setText((position+2)+"楼");
			neirong=pll.getneirong();
			time=pll.getCreatedAt();
			id=pll.getUser().getObjectId();
			gj.setTouxiang(zujian.pl_touxiang,data.get(position).getUser());
			zujian.pl_hfneirong.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{

						for(int i=0;i<data.size();i++){
							if((data.get(i).getObjectId()).equals(data.get(position).getHfneirong().getObjectId())){

								list.setSelection(i+1);
								break;
							}
						}
						// TODO: Implement this method
					}
				});
				
		}else{
			Log.e("Pladapter","执行提醒");
			zujian.pl_lou.setVisibility(View.GONE);
			if(mess==null){
				Log.e("Pladapter","me为空");
			}else{
				Log.e("Pladapter","me不为空");
			}
			gj.setTouxiang(zujian.pl_touxiang,mess.get(position).getSenduser());
			
			MessageAlert me=mess.get(position);
				PingLun pp=me.getHfhf();
				if(pp!=null){
				hfnr=pp.getneirong();
				}
				name=me.getSenduser().getUsername();
				neirong=me.getSendmessage();

				time=me.getCreatedAt();
				//tou=me.getSenduser().getavatar();
				id=me.getSenduser().getObjectId();
				

		}
		if(hfnr==null){
			zujian.pl_hfneirong.setVisibility(View.GONE);
		}else{
			zujian.pl_hfneirong.setVisibility(View.VISIBLE);
			zujian.pl_hfneirong.setText(hfnr);
		}
		
		
		
		zujian.pl_name.setText(name);
		zujian.pl_neirong.setText(neirong);
		
		zujian.pl_time.setText(time);
		
		
		zujian.pl_touxiang.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					
					Intent intent=new Intent(context,YonghuMingpian.class);
		
					if(id==null){
						gj.show("没有该用户的信息");
					}else{
                        intent.putExtra("id",id);
                        context.startActivity(intent);
					}
					
					
					// TODO: Implement this method
				}
			});
		
		
        return convertView;  
	}
	
	

}  

